package dao;

import pojo.UserDO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseDAO {
    public Connection con;
    public PreparedStatement statement;
    public ResultSet rs ;

    public Connection getCon() {
        return con;
    }

    public PreparedStatement getStatement() {
        return statement;
    }

    public ResultSet getRs() {
        return rs;
    }

    public  Connection getConnection() {
        con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/hw?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "kevind110");
            System.out.println("con:" + con);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return con;
    }
    public void close(){
        if(statement != null){
            try {
                rs.close();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public  void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
                con.close();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }
    }

    public  void close(Connection con, Statement statement, ResultSet rs) {
        if (statement != null) {
            try {
                rs.close();
                statement.close();
                con.close();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }
    }
    public void setAutocommit (boolean autocommit ){
        try {
            con.setAutoCommit(autocommit);
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
    }

    public  void commit ()  {
        try {
            con.commit();
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
    }

    public void rollback (){
        try {
            con.rollback();
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
    }


}
